//
//  BaseScrollView.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/8/22.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "BaseScrollView.h"

@interface BaseScrollView()<UIScrollViewDelegate>

@end


@implementation BaseScrollView

- (instancetype) init {
    if (self = [super init]) {
        [self addSubview:self.scrollView];
        self.backgroundColor = [UIColor color_main_background_color];
        [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.scrollView);
            make.height.equalTo(self.scrollView);
        }];
        
        
        if (self.viewsArray.count != 0) {
            [self setViews];
        }
        
        
    }
    return self;
}

- (void)setViewsArray:(NSArray *)viewsArray {
    _viewsArray = viewsArray;
    [self setViews];
}

- (void) setViews {
    UIView *lastAdView = nil;
    isOpen = YES;
    
    for (UIView *adView in self.viewsArray) {
        [self.scrollView addSubview:adView];
        [adView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(lastAdView ? lastAdView.mas_right : @0);
            make.bottom.equalTo(@0);
            make.width.equalTo(self.scrollView.mas_width);
            make.height.equalTo(self.scrollView.mas_height);
        }];
        
        lastAdView = adView;
    }
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lastAdView.mas_right);
    }];
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.backgroundColor = [UIColor color_main_background_color];
        _scrollView.pagingEnabled = YES;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.delegate = self;
        [_scrollView addSubview:self.contentView];
        _scrollView.bounces = NO;
    }
    return _scrollView;
}

- (UIView *)contentView {
    if (_contentView == nil) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = [UIColor color_main_background_color];
    }
    
    return _contentView;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    HXLog(@"sdfsdfsdf");
}

#pragma mark -> scrollView的代理方法
- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat contentX = scrollView.contentOffset.x;
    if (isOpen) {
        if (self.baseScroll) {
            self.baseScroll(scrollView);
        }
    }
}

bool isOpen;
- (void)setSelectIndex:(NSInteger)selectIndex {
    _selectIndex = selectIndex;
    isOpen = NO;
    [UIView animateWithDuration:0.3 animations:^{
        self.scrollView.contentOffset = CGPointMake(self.scrollView.width * selectIndex , 0);
    } completion:^(BOOL finished) {
        isOpen = YES;
    }];
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
